import logging
from datetime import datetime

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import resource_service
from airflow.exceptions import AirflowException
from airflow.models.idata_auth_resource import AuthResource

log = logging.getLogger(__name__)

api_speech_resource = Blueprint('api_speech_resource', __name__)


@api_speech_resource.route('/resources/<int:rid>', methods=['GET'])
def get_resource(rid):
    try:
        resource = AuthResource.get_resource(rid)
        return jsonify(code=0, message='ok', data=AuthResource.to_json(resource))
    except AirflowException as e:
        log.error("get_resource error =%s, params=%s", e, id, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_resource.route('/resources', methods=['GET'])
def get_resources():
    try:
        page = request.args.get('page', 0, type=int)
        if page == 0:
            users = AuthResource.get_resources()
            map_user = map(AuthResource.to_json, users)
            return jsonify(code=0, message='ok', data=list(map_user))
        else:
            resource_result = AuthResource.get_resources_page(page)
            resource_result.items = list(map(AuthResource.to_json, resource_result.items))
            result = resource_result.to_json()
            return jsonify(code=0, message='ok', data=result)
    except AirflowException as e:
        log.error("get_resources error =%s", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_resource.route('/resources', methods=['POST'])
def add_resource():
    try:
        name = request.args.get("name", type=str, default=None)
        url_prefix = request.args.get("url_prefix", type=str, default=None)
        if name is None:
            log.error("add_resource error name=%s", name, )
            return jsonify(code=400, message="add_resource error name=None", data=None)
        if url_prefix is None:
            log.error("add_resource error url_prefix=%s", url_prefix, )
            return jsonify(code=400, message="add_resource error url_prefix=None", data=None)

        resource = AuthResource(name, url_prefix, datetime.now())
        AuthResource.add_resources(resource)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("add_resource error =%s,", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_resource.route('/resources/<int:id>', methods=['DELETE'])
def delete_resource(id):
    try:
        resource_service.delete_resource(id)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("delete_resource error =%s, params=%s", e, id, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_resource.route('/resources', methods=['PUT'])
def update_resource():
    try:
        id = request.args.get("id", type=int, default=None)
        name = request.args.get("name", type=str, default=None)
        url_prefix = request.args.get("url_prefix", type=str, default=None)
        if id is None:
            log.error("update_resource error id=%s", id, )
            return jsonify(code=400, message="add_resource error id=None", data=None)
        if name is None:
            log.error("update_resource error name=%s", name, )
            return jsonify(code=400, message="add_resource error name=None", data=None)
        if url_prefix is None:
            log.error("update_resource error url_prefix=%s", url_prefix, )
            return jsonify(code=400, message="add_resource error url_prefix=None", data=None)

        resource = AuthResource(name, url_prefix, datetime.now())
        resource.id = id
        AuthResource.update_resource(resource)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("update_resource error =%s,", e, )
        return jsonify(code=500, message=str(e), data=None)
